package com.huawei.hwclouds.lubanops.demo.common.biz;

import java.io.Serializable;

import com.huawei.hwclouds.lubanops.demo.common.exception.common.ParameterException;

/**
 * 分页查询的请求的基类，里面封装个页码，每页个数等 参数
 *
 * @author f00384042
 */
public abstract class AbstractPageQueryRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    protected Integer pageNumber = 1;

    protected Integer pageSize = 20; // 默认为20条

    protected Boolean countNeeded = false; // 是否需要返回count

    public Integer getStartNum() {
        if (pageNumber == null || pageNumber <= 0) {
            pageNumber = 1;
        }
        return (pageNumber - 1) * pageSize;
    }

    public Integer getEndNum() {
        return getStartNum() + pageSize;
    }

    public Integer getPageNumber() {

        return pageNumber;
    }

    public void setPageNumber(Integer page) {
        if (page == null || page <= 0) {
            throw new ParameterException("invalid page no:" + page);
        }

        this.pageNumber = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize == null) {
            return;
        }
        if (pageSize <= 0) {
            throw new ParameterException("invalid pageSize:" + pageSize);
        }
        this.pageSize = pageSize;
    }

    public Boolean isPaged() {
        return pageSize != null;
    }

    public Boolean isCountNeeded() {
        return countNeeded;
    }

    /**
     * 当需要统计功能的时候用该方法
     *
     * @param countNeeded
     * @param pageSize
     */
    public void setCountNeeded(Boolean countNeeded, final Integer pageSize) {
        this.countNeeded = countNeeded;
    }

    public void setCountNeeded(Boolean countNeeded) {
        this.countNeeded = countNeeded;
    }

}
